//
// Created by lymk on 2021/8/7.
//

#ifndef GAME2D_GMATH_H
#define GAME2D_GMATH_H
#include "iostream"
namespace GMath{
    enum PolygonRelation{
        INTERSECT,
        CONTAIN,
        DISJOINT,
        BELONG_TO
    };
    enum PointPolyRelation{
        INSIDE_THE_POLY,
        OUTSIDE_THE_POLY,
        ON_THE_POLY
    };
    struct Point{
        float X;
        float Y;
        Point():X(0),Y(0){}
        Point(float x,float y):X(x),Y(y){}
        Point operator+(const Point& p1) const{
            return {this->X+p1.X,this->Y+p1.Y};
        }
        Point operator-(const Point& p1) const{
            return {this->X-p1.X,this->Y-p1.Y};
        }
        Point operator*(const float& p1) const{
            return {this->X*p1,this->Y*p1};
        }
    };
    typedef Point Vec2;
    struct LineSegment{
        Point start;
        Point end;
        LineSegment():start(0,0),end(0,0){}
        LineSegment(Point p1,Point p2):start(p1),end(p2){}
        LineSegment(float sx,float sy,float ex,float ey):start(sx,sy),end(ex,ey){}
    };
    struct Rectangle{
        Point LeftUpper;
        Point BottomRight;
        Rectangle():LeftUpper(0,0),BottomRight(0,0){}
        Rectangle(Point p1,Point p2):LeftUpper(p1),BottomRight(p2){}
        Rectangle(float sx,float sy,float ex,float ey):LeftUpper(sx,sy),BottomRight(ex,ey){}
    };
    struct LineFunction{
        float paramA;
        float paramB;
        float paramC;
        LineFunction():paramA(0),paramB(0),paramC(0){}
        LineFunction(float A,float B,float C):paramA(A),paramB(B),paramC(C){}
        LineFunction(Point p1,Point p2){
            CalculateFunction(p1,p2);
        }
        float CalculateValue(Point p) const{
            return paramA*p.X + paramB*p.Y + paramC;
        }
        void CalculateFunction(Point p1,Point p2){
            paramA = p2.Y - p1.Y;
            paramB = p1.X - p2.X;
            paramC = p2.X*p1.Y - p1.X*p2.Y;
        }
        /*
        bool IsIntersect(Rectangle rect) const{
            float p1 = CalculateValue(rect.start);
            float p2 = CalculateValue(Point(rect.start.X - rect.width,rect.start.Y));
            if((p1>0&&p2<0) || (p1<0&&p2>0))return true;
            float p3 = CalculateValue(Point(rect.start.X,rect.start.Y - rect.height));
            if((p2>0&&p3<0) || (p2<0&&p3>0))return true;
            float p4 = CalculateValue(Point(rect.start.X - rect.width,rect.start.Y-rect.height));
            if((p3>0&&p4<0) || (p3<0&&p4>0))return true;
            std::cout<<p1<<' '<<p2<<' '<<p3<<' '<<p4<<std::endl;
            return false;
        }*/
    };
    PointPolyRelation JudgeRelationBetweenPointAndRectangle(Point p,Rectangle rectangle);
    PolygonRelation JudgeRelationAmongRectangles(Rectangle rectangle1,Rectangle rectangle2);
}

#endif //GAME2D_GMATH_H
